function validateContent() {
    var ctrl = $("#content");
    if ($.trim(ctrl.val()) == '') {
        var l = $("<label class='error'>请提供文章内容</label>");
        l.appendTo(ctrl.parent("td").parent("tr").prev("tr").children("td")[0]);
        l.delay(3000, function() {
            l.remove();
        });
        return false;
    }
    return true;
}

function validateTitle() {
    var ctrl = $("#title");
    if ($.trim(ctrl.val()) == '') {
        var l = $('<label class="error">请提供标题</label>');
        l.appendTo(ctrl.parent("td").parent("tr").prev("tr").children("td")[1]);
        l.delay(3000, function() {
            l.remove();
        });
        return false;
    }
    return true;
}

function setValues(contentTextArea) {
    var data = contentTextArea.nicInstances[0].getContent();
    if ('<br>' == data.toLowerCase()) {
        data = '';
    }
    $('[name=content]').val(data);
    data = data.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
    data = data.replace(/<.*?>/ig, "");
    data = data.replace(/&nbsp;/ig, "");
    data = data.substring(0, 100);
    //TODO: consider how many words to get
    $('[name=abstract]').val(data + '...');
}


function login_form (e) {
    e.preventDefault();
    $('#loginFormWrapper').modal({
        containerCss: {
            height: 205,
            width: 450
        }
    });
}

function login() {
    var userName = $.trim($('#userName').val());
    var password = $.trim($('#password').val());
    if (userName == '' || password == '') {
        var l = $('<label class="error">用户名和密码不能为空</label>');
        l.appendTo($('#errorWrapper'));
        l.delay(3000, function() {
            l.remove();
        });
        return;
    }
    $.post("/account/log_in",
    {
        userName: $.trim($('#userName').val()),
        password: $.trim($('#password').val()),
        remember_me: $('#remember_me').attr('checked')
        },
    showResponse, "json");
}

function digg(id, post_promote) {
    $.post("/post/digg/"+id,
    {
        promote:post_promote
    },
    digged,
    "json");
    $('#d'+id).html('<h3><span>顶..</span></h3>');
    if($('#dig_bottom') != undefined) {
        $('#dig_bottom').html('<div class="digged"> <br/>顶...</div>');
    }
}

function digged(data, textStatus)  {
    if (data.flag == 1) {
        $('#d'+data.id).html(data.html);
        if($('#dig_bottom') != undefined) {
            $('#dig_bottom').html(data.html2);
        }
    }
}

function genCommentDiv(data, content)  {
    var divStr;
    divStr = '<div><div class="comment_title"><div style="float:right;"> ';
    divStr = divStr + data.createDate + '</div><div> ';
    divStr = divStr + data.userInfo + ' ';
    divStr = divStr + '</div></div><div class="comment_content"> ';
    divStr = divStr + content + ' ';
    divStr = divStr + '</div></div> ';
    return divStr;
}
